package com.jxs.android.weiget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.SpannableString;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.telecom.weixi.R;

public class TableFormView extends LinearLayout {
	private TableLayout mTable;
	private TableLayout mTableTitle;
	private Context mContext;

	private static final int TextSize = 13;
	private static final int TitleSize = 14;

	public TableFormView(Context context) {
		this(context, null);
	}

	public TableFormView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setupView(context);
	}

	void setupView(Context context) {
		mContext = context;
		LayoutInflater.from(context).inflate(R.layout.bu_tab_form_view, this);

		mTable = (TableLayout) findViewById(R.id.table_form);
		mTableTitle = (TableLayout) findViewById(R.id.table_title);
	}

	public interface OnCellClickListener {
		void onCellClick(int row, int col);
	}

	OnCellClickListener mCellClickListener;

	public void setOnCellClickListener(OnCellClickListener listener) {
		mCellClickListener = listener;
	}

	int linkedColor;

	public void setLinkedColor(int color) {
		linkedColor = color;
	}

	OnClickListener cellListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (mCellClickListener != null) {
				Bundle data = (Bundle) v.getTag();
				int row = data.getInt(Row_Num);
				int col = data.getInt(Col_Num);
				mCellClickListener.onCellClick(row, col);
			}
		}
	};

	public void setStretchAllColumns(boolean isStrechAll) {
		mTable.setStretchAllColumns(isStrechAll);
		mTableTitle.setStretchAllColumns(isStrechAll);
	}

	public void setColumnShrinkable(int columnIndex, boolean isShrinkable) {
		mTable.setColumnShrinkable(columnIndex, isShrinkable);
		mTableTitle.setColumnShrinkable(columnIndex, isShrinkable);
	}

	public void setColumnStretchable(int columnIndex, boolean isShrinkable) {
		mTable.setColumnStretchable(columnIndex, isShrinkable);
		mTableTitle.setColumnStretchable(columnIndex, isShrinkable);
	}

	public void initTableTitle(List<String> titles) {
		initTableTitle(mTableTitle, titles, false);
		initTableTitle(mTable, titles, true);
	}

	private void initTableTitle(TableLayout table, List<String> titles, boolean isSub) {
		TableRow row = new TableRow(mContext);
		LayoutParams paramLayout = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.FILL_PARENT);
		for (int index = 0; index < titles.size(); index++) {
			TextView txCell = new TextView(mContext);
			txCell.setTextColor(Color.WHITE);
			txCell.setTextSize(TitleSize);
			txCell.setPadding(0, 0, 8, 0);
			txCell.setTextAppearance(mContext, android.R.attr.textAppearanceLarge);
			txCell.setGravity(Gravity.CENTER);
			txCell.setBackgroundResource(R.drawable.row0);
			txCell.setText(titles.get(index));
			row.addView(txCell, paramLayout);
		}
		row.setGravity(Gravity.CENTER);
		TableLayout.LayoutParams lp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		table.addView(row, lp);
		if (isSub) {
			row.setVisibility(View.INVISIBLE);
		}
	}

	private static final String Row_Num = "row_index";
	private static final String Col_Num = "col_index";

	public void resetTableRow() {
		mTable.removeAllViews();
		mTableTitle.removeAllViews();
	}

	public void addTableRow(List<String> rowData, boolean isLightRow) {
		List<Boolean> clickable = new ArrayList<Boolean>();
		for (int index = 0; index < rowData.size(); index++)
			clickable.add(false);
		addTableRow(rowData, clickable, 0, isLightRow);
	}

	public void addTableRow(List<String> rowData, boolean isLightRow, boolean special, int index) {
		List<Boolean> clickable = new ArrayList<Boolean>();
		for (int id = 0; id < rowData.size(); id++)
			clickable.add(false);
		addTableRow(rowData, clickable, 0, isLightRow, special, index);
	}

	public void addTableRow(List<String> rowData, List<Boolean> clickable, int rowIndex, boolean isLightRow, Object... special) {
		addTableRow(mTableTitle, false, rowData, clickable, rowIndex, isLightRow, special);
		addTableRow(mTable, true, rowData, clickable, rowIndex, isLightRow, special);
	}

	private void addTableRow(TableLayout table, boolean isSub, List<String> rowData, List<Boolean> clickable, int rowIndex, boolean isLightRow, Object... special) {
		TableRow row = new TableRow(mContext);
		LayoutParams paramLayout = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.FILL_PARENT);
		for (int index = 0; index < rowData.size(); index++) {
			ClickAbleText txCell = new ClickAbleText(mContext);
			txCell.setTextSize(TextSize);
			if (clickable.get(index)) {
				txCell.setClickAble(true);
				Bundle data = new Bundle();
				data.putInt(Row_Num, rowIndex);
				data.putInt(Col_Num, index);
				txCell.setTag(data);
				txCell.setOnClickListener(cellListener);
				txCell.setLinkedColor(mContext.getResources().getColor(R.color.prompBlue));
				txCell.setTextColor(mContext.getResources().getColor(R.color.prompBlue));
			} else {
				txCell.setClickAble(false);
				txCell.setTextColor(mContext.getResources().getColor(R.color.prompBlack));
			}

			txCell.setPadding(0, 0, 8, 0);
			txCell.setGravity(Gravity.CENTER);

			boolean is = false;
			if (null != special && special.length == 2) {
				if (Integer.valueOf(special[1].toString()) == 0 && Boolean.valueOf(special[0].toString())) {
					// txCell.setTextColor(mContext.getResources().getColor(R.color.white));
					txCell.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));// 加粗
					txCell.getPaint().setFakeBoldText(true);// 加粗is
					is = true;
				} else {
					if (index == 1) {
						txCell.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
					}
				}
			}
			String strContent = rowData.get(index);
			SpannableString content = new SpannableString(is ? strContent.trim() : strContent);
			txCell.setText(content);

			if (isLightRow)
				txCell.setBackgroundResource(R.drawable.table_row0);
			else
				txCell.setBackgroundResource(R.drawable.table_row1);
			row.addView(txCell, paramLayout);
		}
		row.setGravity(Gravity.CENTER);
		TableLayout.LayoutParams lp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		table.addView(row, lp);
		if (!isSub) {
			row.setVisibility(View.INVISIBLE);
		}
	}
}
